package com.shuzixiangcun.controller.front;

import com.shuzixiangcun.entity.Orders;
import com.shuzixiangcun.service.front.OrdersFServiceImpl;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单表(Orders)表控制层
 *
 * @author makejava
 * @since 2024-06-24 08:26:16
 */
@RestController
@RequestMapping("/orders")
public class OrdersFController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersFServiceImpl ordersService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Orders> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.ordersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param orders 实体
     * @return 新增结果
     */


    /**
     * 编辑数据
     *
     * @param orders 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Orders> edit(Orders orders) {
        return ResponseEntity.ok(this.ordersService.update(orders));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.ordersService.deleteById(id));
    }
    @PostMapping("/insertOrder")
    public ResponseEntity insertOrder(Orders orders){
        String orderNID= ordersService.generateOrderNumber();
        orders.setOrderNId(orderNID);
        int num = this.ordersService.insert(orders);
        if (num>0){
            return ResponseEntity.status(200).body("添加成功");
        }
        return ResponseEntity.status(500).body("添加失败");
    }

    @PostMapping("/List")
    public ResponseEntity<List<Orders>> showListByUserId(Integer userId) {

        return ResponseEntity.ok(this.ordersService.showListByUserId(userId));
    }

}

